package golf.stockinfo.simulationweb.servlet;

import golf.stockinfo.buysellsimulation.GlobalCache;
import golf.stockinfo.buysellsimulation.SimulationCommonUtils;
import golf.stockinfo.buysellsimulation.util.DateUtil;
import golf.stockinfo.simulationweb.core.SimulationOutput;
import golf.stockinfo.simulationweb.core.SimulationThread;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Status
 */
@WebServlet("/status")
public class Status extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	public static HashMap<Integer, SimulationThread> ALL_THREADS = new HashMap<Integer, SimulationThread>();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Status() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String threadName = request.getParameter("threadName");
		if(threadName == null || threadName.length() == 0){
			printHtml(response);
		}else{
			for(int simThreadKey : Status.ALL_THREADS.keySet()){
				if(Status.ALL_THREADS.get(simThreadKey).getThreadName().equals(threadName)){
					Status.ALL_THREADS.get(simThreadKey).stopThread();
				}
			}
			printHtml(response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

	private void printHtml(HttpServletResponse response) throws IOException{
		
		response.setContentType("text/html");

		PrintWriter out = response.getWriter();
		
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Simulation Status</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<br><b>======================================================</b>");
		out.println("<br>List of Threads");
		for(int simThreadKey : ALL_THREADS.keySet()){
			out.println("<br>- " + ALL_THREADS.get(simThreadKey).getThreadStatus());
		}
		out.println("<br><b>======================================================</b>");
		out.println("<br>Stop thread");
		out.println("		<form method=\"POST\" action=\"status\">");
		out.println("			<label for=\"name\">Enter Name of thread to stop </label>");
		out.println("			<input type=\"text\" id=\"threadName\" name=\"threadName\"/><br><br>");
		out.println("			<input type=\"submit\" value=\"Submit Form\"/>");
		out.println("			<input type=\"reset\" value=\"Reset Form\"/>");
		out.println("		</form>");
		out.println("<br><b>======================================================</b>");
		out.println("<br>Total profilt count so far: " + SimulationCommonUtils.formatInteger(SimulationThread.TOTAL_PROFIT_COUNT));
		out.println("<br>Total loss count so far: " + SimulationCommonUtils.formatInteger(SimulationThread.TOTAL_LOSS_COUNT));
		out.println("<br><b>======================================================</b>");
		out.println("<br>   List of good result so far....");
		
		synchronized (SimulationThread.ALL_OUTPUT) {
			if(SimulationThread.ALL_OUTPUT.size() > 0){
				for(SimulationOutput simOut : SimulationThread.ALL_OUTPUT){
					out.println("<br>" + simOut.toString());
				}
			}
		}
		out.println("</body>");
		out.println("</html>");
		out.close();
		out.close();
		
	}
}
